package com.dongdongshop.controller;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.config.LoginToken;
import com.dongdongshop.data.Result;
import com.dongdongshop.dto.SellerDTO;
import com.dongdongshop.dto.UserDto;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.enums.TokenEnum;
import com.dongdongshop.exception.LoginException;
import com.dongdongshop.model.TbPerson;
import com.dongdongshop.uitl.JwtUtil;
import com.dongdongshop.uitl.LoginUser;
//import com.dongdongshop.vo.PersonVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Component
public class LoginController {
    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping("/login")
    public Result loginController(@RequestParam String username,@RequestParam String password){
        Subject subject = SecurityUtils.getSubject();
        LoginToken loginToken = new LoginToken(username,password, TokenEnum.ADMIN);
        try {
            subject.login(loginToken);
        }catch (UnknownAccountException | IncorrectCredentialsException e){
            return Result.response(ResultEnums.LOGIN_ERRORNAME);
        }
        //获取person对象
        TbPerson person = (TbPerson) SecurityUtils.getSubject().getPrincipal();
        LoginUser loginUser=new LoginUser();
        loginUser.setUserId(person.getId().toString());
        loginUser.setUserName(person.getName());
        //将person对象放在personVO中
//        PersonVO personVO = new PersonVO();
//        BeanUtils.copyProperties(person,personVO);
        //生成token
        String token = jwtUtil.createJwt(loginUser.getUserId().toString(), JSONObject.toJSONString(loginUser), "login");
        return Result.response(ResultEnums.LOGIN_SUCCESS).setData(token);

    }
    @PostMapping("/sellerLogin")
    public Result sellerLogin(@RequestParam String username,@RequestParam String password){
        Subject subject = SecurityUtils.getSubject();
        LoginToken loginToken = new LoginToken(username,password, TokenEnum.SELLER);
        try {
            subject.login(loginToken);
        }catch (UnknownAccountException | IncorrectCredentialsException e){
            return Result.response(ResultEnums.LOGIN_ERRORNAME);
        }catch (LoginException e){
            return Result.response(ResultEnums.LOGIN_EXCEPTION);
        }
        //获取person对象
        SellerDTO sellerDTO = (SellerDTO) SecurityUtils.getSubject().getPrincipal();
        LoginUser loginUser=new LoginUser();
        loginUser.setUserId(sellerDTO.getSellerId());
        loginUser.setUserName(sellerDTO.getSellerId());
        //生成token
        String token = jwtUtil.createJwt(loginUser.getUserId(), JSONObject.toJSONString(loginUser), "seller");
        return Result.response(ResultEnums.LOGIN_SUCCESS).setData(token);

    }


    @PostMapping("uulogin")
    public Result userLogin(@RequestParam String username, @RequestParam String password){
        // 1,获取subject
        Subject subject = SecurityUtils.getSubject();
        // 2,将用户信息封装到token中
        LoginToken loginToken = new LoginToken(username, password,TokenEnum.USER);
        // 3,调用subject的login方法
        try {
            subject.login(loginToken);
        } catch (UnknownAccountException | IncorrectCredentialsException e){
            return  Result.response(ResultEnums.LOGIN_ERRORNAME);
        }catch (LoginException e){
            return Result.response(ResultEnums.LOGIN_EXCEPTION);
        }

        //登录成功封装 jwt吧并将token返回给前端
        UserDto userDto = (UserDto) SecurityUtils.getSubject().getPrincipal();
        LoginUser loginUser = new LoginUser();
        loginUser.setUserName(userDto.getUsername());
        String str = String.valueOf(userDto.getId());
        loginUser.setUserId(str);
        String token = jwtUtil.createJwt(loginUser.getUserId(), JSONObject.toJSONString(loginUser), "loginUser");
        return  Result.response(ResultEnums.LOGIN_SUCCESS).setData(token);
    }

}
